/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.dispatchers;

import com.sdd.facades.view.CommentViewFacade;
import com.sdd.facades.view.CustomerViewFacade;
import com.sdd.facades.view.ProductViewFacade;
import com.sdd.plains.CommentPlain;
import com.sdd.plains.CustomerPlain;
import com.sdd.plains.ProductPlain;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Carlos
 */
@WebServlet(name = "CommentSaver", urlPatterns = {"/CommentSaver"})
public class CommentSaver extends HttpServlet {

    @EJB
    CustomerViewFacade customerViewFacade;
    @EJB
    CommentViewFacade commentViewFacade;
    @EJB
    ProductViewFacade productViewFacade;
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            CustomerPlain customer = (CustomerPlain) request.getSession().getAttribute("customer");
            if(customer != null) {
                CommentPlain cp = new CommentPlain();
                String id = request.getParameter("idProduct");
                ProductPlain product = productViewFacade.getProduct(new Long(id));
                cp.setCommentator(customer);
                cp.setProduct(product);
                cp.setText(request.getParameter("comment"));
                commentViewFacade.addComment(cp);
                out.print("ok");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher r = getServletContext().getRequestDispatcher("/PruebasComentarios.jsp");
        r.include(request, response);
        ProductPlain product = productViewFacade.getProduct((long)51);// Por ejemplo
        List<CommentPlain> allComments = commentViewFacade.getAllCommentsOfAProduct(product);
        for (int i = 0; i < allComments.size(); i++) {
            CommentPlain cp = allComments.get(i);
            out.println("<p>");
            out.println("<b>"+ cp.getCommentator().getLogin()+" says: </b>");
            out.println("Comment: "+cp.getText());
            out.println("On: "+cp.getDateAdded());
            out.println("</p>");
            
        }
        out.println("</body>");
        out.println("</html>");
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
